// Copyright (C) 1953-2022 NUDT
// Verilog module name - command_parse_and_encapsulate_trl
// Version: V4.0.0.20220526
// Created:
//         by - fenglin 
////////////////////////////////////////////////////////////////////////////
// Description:
//         
///////////////////////////////////////////////////////////////////////////

`timescale 1ns/1ps

module command_parse_and_encapsulate_trl
(
        i_clk     ,
        i_rst_n   ,
        
        iv_addr       ,                         
        iv_wdata      ,                        
        i_wr          ,         
        i_rd          ,         
                      
        o_wr          ,          
        ov_addr       ,       
        ov_rdata      ,

        ov_token_bucket_size       ,            
        ov_add_tokens_num_perround 
       
);

// I/O
// clk & rst
input                   i_clk;
input                   i_rst_n;

input       [18:0]      iv_addr;                         
input       [31:0]      iv_wdata;                        
input                   i_wr    ;         
input                   i_rd    ;         

output reg              o_wr    ;          
output reg [18:0]       ov_addr ;       
output reg [31:0]       ov_rdata     ;

output reg [15:0]       ov_token_bucket_size;
output reg [15:0]       ov_add_tokens_num_perround;            
always@(posedge i_clk or negedge i_rst_n)begin
    if(!i_rst_n) begin
        o_wr           <= 1'b0;
        ov_addr        <= 19'b0;
        ov_rdata       <= 32'b0;
        
        ov_token_bucket_size        <= 16'h7fff;
        ov_add_tokens_num_perround  <= 16'h3e8 ;//1000d
    end
    else begin
        if(i_wr)begin//write
            if(iv_addr == 19'd0)begin 
                ov_token_bucket_size        <= iv_wdata[15:0];           
                
                o_wr           <= 1'b0;
                ov_addr        <= 19'b0;
                ov_rdata       <= 32'b0;
            end
            else if(iv_addr == 19'd1)begin
                ov_add_tokens_num_perround        <= iv_wdata[15:0];           
                
                o_wr           <= 1'b0;
                ov_addr        <= 19'b0;
                ov_rdata       <= 32'b0;            
            end
            else begin
                o_wr           <= 1'b0;
                ov_addr        <= 19'b0;
                ov_rdata       <= 32'b0;               
            end
        end
        else if(i_rd)begin//read
            if(iv_addr == 19'd0)begin
                o_wr           <= 1'b1;
                ov_addr        <= iv_addr;
                ov_rdata       <= {16'b0,ov_token_bucket_size};
            end
            else if(iv_addr == 19'd1)begin
                o_wr           <= 1'b1;
                ov_addr        <= iv_addr;
                ov_rdata       <= {16'b0,ov_add_tokens_num_perround};
            end            
            else begin
                o_wr           <= 1'b1;
                ov_addr        <= iv_addr;
                ov_rdata       <= 32'hffffffff;            
            end
        end
        else begin
            o_wr           <= 1'b0;
            ov_addr        <= 19'b0;
            ov_rdata       <= 32'b0;
        end        
    end
end       
endmodule